package fleporcq.simpleReflect;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class MethodDescriptor {

    protected Method method;

    protected static final String PARAMS_SEPARATOR = ", ";

    public MethodDescriptor(Method method) {
        this.method = method;
    }

    public String getName() {
        return this.method.getName();
    }

    public EVisibility getVisibility() {
        int modifier = this.method.getModifiers();
        if (Modifier.isPublic(modifier)) {
            return EVisibility.PUBLIC;
        } else if (Modifier.isPrivate(modifier)) {
            return EVisibility.PRIVATE;
        } else if (Modifier.isProtected(modifier)) {
            return EVisibility.PROTECTED;
        }
        return null;
    }

    public boolean isPublic() {
        int modifier = this.method.getModifiers();
        return Modifier.isPublic(modifier);
    }

    public boolean isPrivate() {
        int modifier = this.method.getModifiers();
        return Modifier.isPrivate(modifier);
    }

    public boolean isProtected() {
        int modifier = this.method.getModifiers();
        return Modifier.isProtected(modifier);
    }

    public String getReturnType() {
        return this.method.getReturnType().getSimpleName();
    }

    public String getParameterTypes() {
        Class[] parameterTypes = this.method.getParameterTypes();
        StringBuilder parameterTypesBuilder = new StringBuilder();
        for (Class parameterType : parameterTypes) {
            parameterTypesBuilder.append(parameterType.getSimpleName());
            parameterTypesBuilder.append(PARAMS_SEPARATOR);
        }
        int length = parameterTypesBuilder.length();
        if (length > 0) {
            parameterTypesBuilder.delete(length - PARAMS_SEPARATOR.length(), length);
        }
        return parameterTypesBuilder.toString();
    }

    @Override
    public String toString() {
        return this.getVisibility() + " " + this.getName() + "(" + this.getParameterTypes() + ") : " + this.getReturnType();
    }


}
